<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="goods_type" label="">
				<uni-easyinput placeholder="商品编号" type="number" v-model="formData.goods_type"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="icon" label="">
				<!-- icon图片展示和上传 -->
				<upload-image :data="formData.icon" @setData="setForm"></upload-image>
				<!-- <uni-easyinput placeholder="商品图标" v-model="formData.icon"></uni-easyinput> -->
			</uni-forms-item>
			<uni-forms-item name="name" label="">
				<uni-easyinput placeholder="商品名称" v-model="formData.name"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/category.js';
	import uploadImage from '../../components/upload-image/upload-image.vue'
	

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'category';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"goods_type": null,
				"icon": "",
				"name": ""
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		components:{
			uploadImage
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 上传图标到云数据库
			 */
			setForm(data) {
				this.formData.icon = data
			},
			// update() {
			// 	new Promise((resolve, reject) => {
			// 		//选择本地图标
			// 		uni.chooseImage({
			// 			count: 1,
			// 			success(res) {
			// 				if (res.tempFilePaths.length > 0) {
			// 					uni.showLoading({
			// 						title: '上传中...'
			// 					})
			// 					let filePath = res.tempFilePaths[0]
			// 					// 阿里云 上传图标到云存储空间
			// 					uniCloud.uploadFile({
			// 						filePath: filePath,
			// 						cloudPath: Date.now() + '.jpg',
			// 						success(res) {
			// 							resolve(res.fileID)
			// 						},
			// 						fail(err) {
			// 							reject(new Error('fail_upload'))
			// 						},
			// 						complete() {
			// 							uni.hideLoading()
			// 						}
			// 					})
			// 				}
			// 			}
			// 		})
			// 	}).then(res=>{
			// 		uniCloud.getTempFileURL({
			// 			fileList:[res],
			// 			success(res) {
			// 			this.formData.icon=res.fileList[0].tempFileURL	
			// 			},
			// 			fail() {
			// 				uni.showModal({
			// 					content:"获取临时链接失败",
			// 					showCancel:false
			// 				})
			// 			}
			// 		})
			// 	})
			// },
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field("goods_type,icon,name").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
